package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 表
 */
@RestController
@RequestMapping("/report")
public class MemberController {
    @Autowired
    private MemberService memberService;
    /**
     * 会员统计折线表
     * @return
     */
    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        //获取当前月份
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        List<String> list =new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);//获取当前日期的后一个月的日期
            list.add(new SimpleDateFormat("yyyy.MM").format(calendar.getTime()));//获取当前日期并获取年月
        }
        //创建map集合 封装月份和每月的会员数量
        Map<String ,Object> map=new HashMap<>();
        //key 与前端的数据类型一致
        map.put("months",list);
        List<Integer> count = this.memberService.getMemberReport(list);
        map.put("memberCount",count);
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }
}
